{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-08T07:38:36.520358Z",
     "start_time": "2025-07-08T07:38:31.498200Z"
    }
   },
   "source": [
    "import skmob\n",
    "from skmob.tessellation import tilers\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from skmob.preprocessing import filtering\n"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 读取基础数据（轨迹数据与东京行政区划的json数据）",
   "id": "2d1108ea0b4c24fc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-08T07:38:50.371421Z",
     "start_time": "2025-07-08T07:38:44.800410Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# read the trajectory data (tokyo-openpflow)\n",
    "\n",
    "df1 = pd.read_csv(\"./trajectory.csv\", sep=',')\n",
    "df = df1.drop(\"Unnamed: 0\", axis=1)\n",
    "tdf = skmob.TrajDataFrame(df, latitude='lat', longitude='lon', user_id='user', datetime='datetime')\n",
    "print(tdf.head())"
   ],
   "id": "4c506acd485a1168",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   uid            datetime        lng        lat\n",
      "0   40 2017-01-12 03:00:00  139.60923  35.641745\n",
      "1   40 2017-01-12 04:00:00  139.60923  35.641745\n",
      "2   40 2017-01-12 05:00:00  139.60923  35.641745\n",
      "3   40 2017-01-12 06:00:00  139.60923  35.641745\n",
      "4   40 2017-01-12 07:00:00  139.60923  35.641745\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-08T09:06:02.934474Z",
     "start_time": "2025-07-08T07:38:52.072093Z"
    }
   },
   "cell_type": "code",
   "source": [
    "baseshape = gpd.read_file(\"./data/tokyo.json\")\n",
    "tessellation = tilers.tiler.get(\"squared\", base_shape=baseshape, meters=100)\n"
   ],
   "id": "21d7b73bcf222269",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 将轨迹数据匹配到各个区划中",
   "id": "f857fe2768bc79ea"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-08T09:10:55.011734Z",
     "start_time": "2025-07-08T09:07:04.734158Z"
    }
   },
   "cell_type": "code",
   "source": "mtdf = tdf.mapping(tessellation)",
   "id": "50534f5edd141fae",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Environment\\Envs\\python3.10.6\\lib\\site-packages\\pyproj\\crs\\crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  in_crs_string = _prepare_from_proj_string(in_crs_string)\n",
      "D:\\Environment\\Envs\\python3.10.6\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3577: FutureWarning: The `op` parameter is deprecated and will be removed in a future release. Please use the `predicate` parameter instead.\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n",
      "D:\\Environment\\Envs\\python3.10.6\\lib\\site-packages\\skmob\\core\\trajectorydataframe.py:322: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.\n",
      "Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.\n",
      "\n",
      "Left CRS: +init=epsg:4326 +type=crs\n",
      "Right CRS: EPSG:4326\n",
      "\n",
      "  tile_ids = gpd.sjoin(gdf, tessellation, how=how, op='within')[[constants.TILE_ID]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-01T10:23:43.315134Z",
     "start_time": "2025-07-01T10:23:43.298022Z"
    }
   },
   "cell_type": "code",
   "source": "print(mtdf.head())",
   "id": "9afa31eb7b208a56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   uid            datetime        lng        lat tile_ID\n",
      "0   40 2017-01-12 03:00:00  139.60923  35.641745      27\n",
      "1   40 2017-01-12 04:00:00  139.60923  35.641745      27\n",
      "2   40 2017-01-12 05:00:00  139.60923  35.641745      27\n",
      "3   40 2017-01-12 06:00:00  139.60923  35.641745      27\n",
      "4   40 2017-01-12 07:00:00  139.60923  35.641745      27\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 计算各个区划的流量流向",
   "id": "80ed1a675a2b18c1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-08T09:17:34.957657Z",
     "start_time": "2025-07-08T09:13:39.124269Z"
    }
   },
   "cell_type": "code",
   "source": "fdf = tdf.to_flowdataframe(tessellation=tessellation, self_loops=True)",
   "id": "ed8d36de8ccf5868",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Environment\\Envs\\python3.10.6\\lib\\site-packages\\pyproj\\crs\\crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  in_crs_string = _prepare_from_proj_string(in_crs_string)\n",
      "C:\\Users\\23513\\AppData\\Local\\Temp\\ipykernel_31600\\3042352916.py:1: FutureWarning: The `op` parameter is deprecated and will be removed in a future release. Please use the `predicate` parameter instead.\n",
      "  fdf = tdf.to_flowdataframe(tessellation=tessellation, self_loops=True)\n",
      "D:\\Environment\\Envs\\python3.10.6\\lib\\site-packages\\skmob\\core\\trajectorydataframe.py:322: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.\n",
      "Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.\n",
      "\n",
      "Left CRS: +init=epsg:4326 +type=crs\n",
      "Right CRS: EPSG:4326\n",
      "\n",
      "  tile_ids = gpd.sjoin(gdf, tessellation, how=how, op='within')[[constants.TILE_ID]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-01T10:13:31.816708Z",
     "start_time": "2025-07-01T10:13:31.799709Z"
    }
   },
   "cell_type": "code",
   "source": "print(fdf)",
   "id": "b1f3eb3af7ba3e32",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    origin destination  flow\n",
      "0        0           0   174\n",
      "1        0           1     4\n",
      "2        0           6    15\n",
      "3        1           0     3\n",
      "4        1           1    74\n",
      "..     ...         ...   ...\n",
      "111      7           7  1030\n",
      "112      7           8    10\n",
      "113      8           2     3\n",
      "114      8           7     3\n",
      "115      8           8   308\n",
      "\n",
      "[116 rows x 3 columns]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    },
    "ExecuteTime": {
     "start_time": "2025-07-08T09:17:40.798754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "m = fdf.plot_flows(flow_color='red', tiles='OpenStreetMap')\n",
    "m"
   ],
   "id": "d17b5ad8f0833412",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
